<?php
include_once('db.php');
$uptypes=array(
    'image/jpg',
    'image/jpeg',
    'image/png',
    'image/pjpeg',
    'image/gif',
    'image/bmp',
    'image/x-png'
);
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE
$destination_folder="../upload/"; //上传文件路径
$cun="../upload/";
$imgpreview=0;      //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2; 
if ($_SERVER['REQUEST_METHOD'] == 'POST')
 {
     if (!is_uploaded_file($_FILES["image"][tmp_name]))
     //是否存在文件
     {
         echo "图片不存在!";
         exit;
     }  
 
    $file = $_FILES["image"];
    if($max_file_size < $file["size"])
    //检查文件大小
    {
        echo "文件太大!";
        exit;
    }  
 
    if(!in_array($file["type"], $uptypes))
    //检查文件类型
    {
        echo "文件类型不符!".$file["type"];
        exit;
    }  
 
    if(!file_exists($destination_folder))
    {
        mkdir($destination_folder);
    }  
 
    $filename=$file["tmp_name"];
    $image_size = getimagesize($filename);
    $pinfo=pathinfo($file["name"]);
    $ftype=$pinfo['extension'];
    $destination = $destination_folder.time().".".$ftype;
    if (file_exists($destination) && $overwrite != true)
    {
        echo "同名文件已经存在了";
        exit;
    }  
 
    if(!move_uploaded_file ($filename, $destination))
    {
        echo "移动文件出错";
        exit;
    }  
 
    if($imgpreview==1)
    {
    echo "<br>图片预览:<br>";
    echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
    echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";
    }
  }
echo "</br>";
$username_admin = $_POST["username_admin"];
$password_admin = $_POST["password_admin"];
$logonname_admin = $_POST["logonname_admin"];
if($logonname_admin==""){
	$logonname_admin = $username_admin;
}
$email = $_POST["email_admin"];
$imagearr = explode("/",$destination);
$image = $imagearr[sizeof($imagearr)-1];
$sql = "select * from users where username='".$username_admin."';";
$result = mysqli_query($link,$sql);
$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
if($rows!=[]&&$rows!=null&&$rows!=""&&$rows!=0){
	echo '<script>alert("注册失败，当前用户名已被注册");window.history.go(-1)</script>';
}else{
	$password_admin = md5($password_admin);
	$sql = "insert into users values(0,'{$username_admin}','{$password_admin}',8888,'{$email_admin}','{$logonname_admin}','{$image}',0);";
	$result = mysqli_query($link,$sql);
	$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
	echo '<script>alert("注册成功");location.href="/home/yi"</script>';
}